
export default {
	namespaced: true,
	state: {
		userId: '',
		username: '',
		password: '',
		uMsg: '',
		pMsg: '',
	},
	getters: {
		isLogin(state) {
			if (!state.userId || typeof(state.userId) === "undefined") {
				return false
			}
			return true
		}
	},
	mutations: {
		setUserId(state, val) {
			state.userId = val
		},
		setUsername(state, val) {
			state.username = val
		},
		setPassword(state, val) {
			state.password = val
		},
		setUMsg(state, val) {
			state.uMsg = val
		},
		setPMsg(state, val) {
			state.pMsg = val
		}
	},
	actions: { 
		login(content) {
			return new Promise((resolve) => {
				const state = content.state
							
				this._vm.$myRequest({
					url: '/sysUser/login',
					data: {
						username: state.username,
						password: state.password
					},
					method: 'POST'
				}).then(res => {
					const data = res.data
					
					if (data.code === 200) {
						state.userId = data.result.id
						state.username = data.result.username
						state.password = ''
						
						resolve({
							status: 0,
							user: {
								userId: data.result.id,
								username: data.result.username
							}
						})
					} else {
						if (data.result != null) {
							// 登录失败
							state.uMsg = data.result.username ? data.result.username : ''
							state.pMsg = data.result.password ? data.result.password : ''
						} else {
							// 没有此用户
							state.username = ''
							state.password = ''
							state.uMsg = ''
							state.pMsg = ''
						}
						resolve({
							status: 1
						})
					}	
				})
			})
		}
	}
}